package com.itheima.mybatis.framework;

import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author ：sean
 * @date ：Created in 2020/7/26
 * @description ：Mapper代理实现
 * @version: 1.0
 */
public class HmMapperProxy implements InvocationHandler, Serializable {
  private  HmSqlSession sqlSession;
  private  Class mapperInterface;

  public HmMapperProxy(HmSqlSession sqlSession, Class mapperInterface) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    // 动态实现方法内容
    String pre = mapperInterface.getName()+"."+method.getName();
    System.out.println(pre);
    if(pre.contains("toString")){
      return "toString:"+proxy.getClass().getName();
    }
    List objects = sqlSession.selectList(pre);
    // 动态方法的返回值
    return objects;
  }
}
